package com.tms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tms.domain.Workcell;
import com.tms.mapper.develop.WorkcellMapper;
import com.tms.service.WorkcellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工作部门业务实现
 *
 * @author jackie
 */
@Service
public class WorkcellServiceImpl implements WorkcellService {

    @Autowired
    private WorkcellMapper workcellMapper;

    @Override
    @Cacheable(cacheNames = "workcellList", key = "#page + #pageSize", unless = "#result == null")
    public PageInfo<Workcell> findWorkcellByPage(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Workcell> list = workcellMapper.selectAllWorkcells();
        return new PageInfo<>(list);
    }

    @Override
    @Cacheable(cacheNames = "workcellAlls", key = "'findAlls'", unless = "#result == null")
    public List<Workcell> findAllWorkcells() {
        return workcellMapper.selectAllWorkcells();
    }

    @Override
    @Cacheable(cacheNames = "workcells", key = "#name", unless = "#result == null")
    public Workcell findWorkcellByName(String name) {
        return workcellMapper.selectByName(name);
    }

    @Override
    @Cacheable(cacheNames = "workcells", key = "#id", unless = "#result == null")
    public Workcell findWorkcellById(Integer id) {
        return workcellMapper.selectByPrimaryKey(id);
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "workcellList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "workcellAlls", allEntries = true, beforeInvocation = true)
            },
            put = {
                    @CachePut(cacheNames = "workcells", key = "#result.id", unless = "#result == null || #result.id == 0"),
                    @CachePut(cacheNames = "workcells", key = "#workcell.name", unless = "#result == null || #result.id == 0")
            }
    )
    public Workcell createWorkcell(Workcell workcell) {
        workcellMapper.insertSelective(workcell);
        return workcell;
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "workcellList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "workcellAlls", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "workcells", key = "#workcell.id"),
                    @CacheEvict(cacheNames = "workcells", key = "#workcell.name")
            }
    )
    public void deleteWorkcellById(Workcell workcell) {
        workcellMapper.deleteByPrimaryKey(workcell.getId());
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "workcellList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "workcellAlls", allEntries = true, beforeInvocation = true)
            },
            put = {
                    @CachePut(cacheNames = "workcells", key = "#workcell.id"),
                    @CachePut(cacheNames = "workcells", key = "#workcell.name")
            }
    )
    public Workcell updateWorkcellById(Workcell workcell) {
        int result = workcellMapper.updateByPrimaryKeySelective(workcell);
        if (result == 0) {
            throw new RuntimeException("更新失败，更新结果数为0");
        }
        return workcell;
    }
}